﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using OF.Component.BLL;
using OF.Component.Model;
using OF.Utility;
using User.Api.Common;
using User.Api.Models;
using AutoMapper;
using OF.Component.Enum;

namespace User.Api.Controllers
{
    /// <summary>
    /// 用户认证接口
    /// </summary>
    public class UserAuthController : BaseApiController
    {
        /// <summary>
        /// 添加证件
        /// </summary>
        /// <param name="reqInfo">请求参数</param>
        /// <returns></returns>
        [HttpPost]
        public ResultData<ResponseUserAuthResult> Add(RequestUserAuthAdd reqInfo)
        {
            var result = ResultData<ResponseUserAuthResult>.Error("");
            if (reqInfo.UserID <= 0)
            {
                throw new BizException(StatusCodeEnum.ParamError);
            }
            var oInfo = Mapper.Map<UserAuthInfo>(reqInfo);
            oInfo.IsActive = 1;
            oInfo.AuthLevel = (int)AuthLevelEnum.IDCard;
            oInfo.CertificateCreateDate = DateTime.Now;
            oInfo.VideoCreatedDate = DateTime.Now;
            var nInsertID = UserAuthOperate.Insert(oInfo);
            UserOperate.UpdateUserLevel(reqInfo.UserID, 1);

            var data = new ResponseUserAuthResult() { UserAuthID = nInsertID, AuthLevel = 1 };
            result = ResultData<ResponseUserAuthResult>.Success(data);
            return result;
        }

        /// <summary>
        /// 添加视频认证
        /// </summary>
        /// <param name="reqInfo">请求参数</param>
        /// <returns></returns>
        [HttpPost]
        public ResultData<ResponseUserAuthResult> AddVideo(RequestUserAuthVideoAdd reqInfo)
        {
            var result = ResultData<ResponseUserAuthResult>.Error("");
            UserAuthInfo oExistedUserAuth;
            if (reqInfo.UserAuthID > 0)
            {
                oExistedUserAuth = UserAuthOperate.GetByID(reqInfo.UserAuthID);
            }
            else
            {
                oExistedUserAuth = UserAuthOperate.GetByUser(reqInfo.UserID);
            }

            if (oExistedUserAuth == null)
            {
                throw new BizException(StatusCodeEnum.UserAuthNotExits);
            }
            reqInfo.UserAuthID = oExistedUserAuth.UserAuthID;

            //更新操作
            Mapper.Map<RequestUserAuthVideoAdd, UserAuthInfo>(reqInfo, oExistedUserAuth);
            oExistedUserAuth.VideoCreatedDate = DateTime.Now;
            if (oExistedUserAuth.AuthLevel == (int)AuthLevelEnum.ASR)
            {
                oExistedUserAuth.AuthLevel = (int)AuthLevelEnum.Complete;
            }
            else
            {
                oExistedUserAuth.AuthLevel = (int)AuthLevelEnum.Vedio;
            }
            UserAuthOperate.Update(oExistedUserAuth);
            UserOperate.UpdateUserLevel(reqInfo.UserID, oExistedUserAuth.AuthLevel);

            var data = new ResponseUserAuthResult() { UserAuthID = oExistedUserAuth.UserAuthID, AuthLevel = oExistedUserAuth.AuthLevel };
            result = ResultData<ResponseUserAuthResult>.Success(data);
            return result;
        }

        /// <summary>
        /// 添加语音识别
        /// </summary>
        /// <param name="reqInfo">请求参数</param>
        /// <returns></returns>
        [HttpPost]
        public ResultData<ResponseUserAuthResult> AddAsr(RequestUserAuthASR reqInfo)
        {
            var result = ResultData<ResponseUserAuthResult>.Error("");
            UserAuthInfo oExistedUserAuth;
            if (reqInfo.UserAuthID > 0)
            {
                oExistedUserAuth = UserAuthOperate.GetByID(reqInfo.UserAuthID);
            }
            else
            {
                oExistedUserAuth = UserAuthOperate.GetByUser(reqInfo.UserID);
            }

            if (oExistedUserAuth == null)
            {
                throw new BizException(StatusCodeEnum.UserAuthNotExits);
            }
            
            oExistedUserAuth.VideoCreatedDate = DateTime.Now;
            if (oExistedUserAuth.AuthLevel == (int)AuthLevelEnum.Vedio)
            {
                oExistedUserAuth.AuthLevel = (int)AuthLevelEnum.Complete;
            }
            else
            {
                oExistedUserAuth.AuthLevel = (int)AuthLevelEnum.ASR;
            }
            UserAuthOperate.Update(oExistedUserAuth);
            UserOperate.UpdateUserLevel(reqInfo.UserID, oExistedUserAuth.AuthLevel);

            var data = new ResponseUserAuthResult() { UserAuthID = oExistedUserAuth.UserAuthID, AuthLevel = oExistedUserAuth.AuthLevel };
            result = ResultData<ResponseUserAuthResult>.Success(data);
            return result;
        }

        /// <summary>
        /// 更新接口
        /// </summary>
        /// <param name="reqInfo">请求参数</param>
        /// <returns></returns>
        [HttpPost]
        public ResultData<ResponseCommon<long>> Update(RequestUserAuthUpdate reqInfo)
        {
            var result = ResultData<ResponseCommon<long>>.Error("");
            UserAuthInfo oExistedUserAuth;
            if (reqInfo.UserAuthID > 0)
            {
                oExistedUserAuth = UserAuthOperate.GetByID(reqInfo.UserAuthID);
            }
            else
            {
                oExistedUserAuth = UserAuthOperate.GetByUser(reqInfo.UserID);
            }

            if (oExistedUserAuth == null)
            {
                throw new BizException(StatusCodeEnum.UserAuthNotExits);
            }
            
            //更新操作
            Mapper.Map<RequestUserAuthUpdate, UserAuthInfo>(reqInfo, oExistedUserAuth);
            UserAuthOperate.Update(oExistedUserAuth);
            result = ResultData<ResponseCommon<long>>.Success(new ResponseCommon<long>(reqInfo.UserAuthID));
            return result;
        }

        /// <summary>
        /// 用户认证信息获取
        /// </summary>
        /// <param name="userAuthID">用户认证信息ID</param>
        /// <returns></returns>
        /// <exception cref="BizException"></exception>
        [HttpGet]
        public ResultData<ResponseUserAuthGet> Get(int userAuthID)
        {
            var result = ResultData<ResponseUserAuthGet>.Error("");
            if (userAuthID <= 0)
            {
                throw new BizException(StatusCodeEnum.ParamError);
            }
            var oInfo = UserAuthOperate.GetByID(userAuthID);
            if (oInfo != null)
            {
                var oRes = Mapper.Map<ResponseUserAuthGet>(oInfo);
                result = ResultData<ResponseUserAuthGet>.Success(oRes);
            }
            else
            {
                throw new BizException(StatusCodeEnum.NoExistInfo);
            }
            return result;
        }

        /// <summary>
        /// 获取授权信息
        /// </summary>
        /// <param name="userId">用户id</param>
        /// <returns></returns>
        [HttpGet]
        public ResultData<ResponseUserAuthGet> GetByUser(long userId)
        {
            var result = ResultData<ResponseUserAuthGet>.Error("");
            if (userId <= 0)
            {
                throw new BizException(StatusCodeEnum.ParamError);
            }
            var oInfo = UserAuthOperate.GetByUser(userId);
            if (oInfo != null)
            {
                var oRes = Mapper.Map<ResponseUserAuthGet>(oInfo);
                result = ResultData<ResponseUserAuthGet>.Success(oRes);
            }
            else
            {
                throw new BizException(StatusCodeEnum.NoExistInfo);
            }
            return result;
        }
    }
}